/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simplemetrosystem.view.canvas.MouseHandlers;

import java.awt.Cursor;
import java.awt.event.MouseEvent;
import simplemetrosystem.control.ElementManager;
import simplemetrosystem.control.EditCommandManager;
import simplemetrosystem.view.canvas.CanvasModel;
import simplemetrosystem.view.MetroFrame;
import simplemetrosystem.view.canvas.MetroCanvas;

/**
 *
 * @author Eric Qian
 */
public class NewStationHandler extends BasicMouseHandler {

    public NewStationHandler(MetroFrame frame) {
        super(frame);
        this.frame = frame;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        super.mouseClicked(e);
        ElementManager elementManager = ElementManager.getInstance();
        EditCommandManager editCommandManager = EditCommandManager.getInstance();
        CanvasModel model = CanvasModel.getInstance();
        MetroCanvas canvas = frame.getMetroTabbedPane().getAllLineCanvas();

        int x = MouseTrans.X(e.getX());
        int y = MouseTrans.Y(e.getY());

        if (model.isStationAvailable(x, y)) {
            editCommandManager.addStation("station" +
                    (elementManager.getStationsSize() + 1), x, y);
        } else if (!model.isEmpty(x, y) && model.isSelectedEmpty(x, y)) {
            model.setSelect();
            canvas.setSelectedMouseHandler();
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        super.mouseEntered(e);
        frame.setCursor(crossCursor);
    }

    @Override
    public void mouseExited(MouseEvent e) {
        super.mouseExited(e);
        frame.setCursor(defaultCursor);
        frame.getMetroTabbedPane().getAllLineCanvas().setNewStation(false);
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        super.mouseMoved(e);
        CanvasModel model = CanvasModel.getInstance();
        MetroCanvas canvas = frame.getMetroTabbedPane().getAllLineCanvas();
        int x = MouseTrans.X(e.getX());
        int y = MouseTrans.Y(e.getY());

        if (model.isStationAvailable(x, y)) {
            canvas.setNewStation(true);
            canvas.setNewStationRec(e.getX(), e.getY());
        } else {
            canvas.setNewStation(false);
        }
    }
    private MetroFrame frame;
    private static Cursor crossCursor = new Cursor(Cursor.CROSSHAIR_CURSOR);
}
